package csel.view.dialog;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.HashMap;
import java.util.LinkedList;
import csel.controller.bindings.Bindable;
import csel.controller.enums.ActionEnum;

import javax.swing.*;

public class ChangeControlsDialog extends JFrame
{
	
	private LinkedList<JComboBox> myComboBoxes;
	private LinkedList<JLabel> myControlLabels;
	private LinkedList<JButton> myButtons;
	private JScrollPane myScrollPane;
	private HashMap<String,Integer> VKDecoder;
	private Bindable myBindable;
	private ActionListener myListener;
	private ActionListener myManager;
	
	public ChangeControlsDialog(Bindable b, ActionListener myListener)
	{	
		myBindable = b;
		defineVKDecoder();
		
		myManager = new ControlDialogListener();
		this.myListener = myListener;
		
		myComboBoxes = new LinkedList<JComboBox>();
		myControlLabels = new LinkedList<JLabel>();
		myButtons = new LinkedList<JButton>();
		
		JComboBox myCBox;
		for (int a = 0; a < 8; ++a)
		{
			myCBox = new JComboBox();
			myCBox.addItem("a");
			myCBox.addItem("b");
			myCBox.addItem("c");
			myCBox.addItem("d");
			myCBox.addItem("f");
			myCBox.addItem("g");
			myCBox.addItem("h");
			myCBox.addItem("j");
			myCBox.addItem("k");
			myCBox.addItem("l");
			myCBox.addItem("m");
			myCBox.addItem("n");
			myCBox.addItem("o");
			myCBox.addItem("p");
			myCBox.addItem("q");
			myCBox.addItem("s");
			myCBox.addItem("u");
			myCBox.addItem("v");
			myCBox.addItem("w");
			myCBox.addItem("x");
			myCBox.addItem("y");
			myCBox.addItem("z");
			myComboBoxes.add(myCBox);
		}
		
		JButton myButton = new JButton("Change");
		myButton.setActionCommand("north");
		myButton.addActionListener(myManager);
		myButtons.add(myButton);
		
		myButton = new JButton("Change");
		myButton.setActionCommand("northeast");
		myButton.addActionListener(myManager);
		myButtons.add(myButton);
		
		myButton = new JButton("Change");
		myButton.setActionCommand("east");
		myButton.addActionListener(myManager);
		myButtons.add(myButton);
		
		myButton = new JButton("Change");
		myButton.setActionCommand("southeast");
		myButton.addActionListener(myManager);
		myButtons.add(myButton);
		
		myButton = new JButton("Change");
		myButton.setActionCommand("south");
		myButton.addActionListener(myManager);
		myButtons.add(myButton);
		
		myButton = new JButton("Change");
		myButton.setActionCommand("southwest");
		myButton.addActionListener(myManager);
		myButtons.add(myButton);
		
		myButton = new JButton("Change");
		myButton.setActionCommand("west");
		myButton.addActionListener(myManager);
		myButtons.add(myButton);
		
		myButton = new JButton("Change");
		myButton.setActionCommand("northwest");
		myButton.addActionListener(myManager);
		myButtons.add(myButton);
		
		myControlLabels.add(new JLabel("Move North"));
		myControlLabels.add(new JLabel("Move Northeast"));
		myControlLabels.add(new JLabel("Move East"));
		myControlLabels.add(new JLabel("Move Southeast"));
		myControlLabels.add(new JLabel("Move South"));
		myControlLabels.add(new JLabel("Move Southwest"));
		myControlLabels.add(new JLabel("Move West"));
		myControlLabels.add(new JLabel("Move Northwest"));

		/*JPanel leftPanel = new JPanel();
		JPanel centerPanel = new JPanel();
		JPanel rightPanel = new JPanel();
		
		leftPanel.setLayout(new BoxLayout(leftPanel,BoxLayout.PAGE_AXIS));
		centerPanel.setLayout(new BoxLayout(centerPanel,BoxLayout.PAGE_AXIS));
		rightPanel.setLayout(new BoxLayout(rightPanel,BoxLayout.PAGE_AXIS));
		*/
		
		JPanel wholePanel = new JPanel();
		wholePanel.setLayout(new GridLayout(8,3));
		for (int a = 0; a < 8; ++a)
		{
			wholePanel.add(myControlLabels.get(a));
			wholePanel.add(myComboBoxes.get(a));
			wholePanel.add(myButtons.get(a));
		}
		
		/*JButton mahButtin = new JButton("Touch Mah Bu'in");
		mahButtin.setActionCommand(StringActionCommands.HIDE_DIALOG);
		mahButtin.addActionListener(myListener);*/
		

		
	
		
		/*wholePanel.add(leftPanel,BorderLayout.WEST);
		wholePanel.add(centerPanel,BorderLayout.CENTER);
		wholePanel.add(rightPanel,BorderLayout.EAST);*/
		//wholePanel.add(mahButtin,BorderLayout.SOUTH);
		wholePanel.setPreferredSize(new Dimension(400,400));
		myScrollPane = new JScrollPane(wholePanel,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		myScrollPane.setPreferredSize(new Dimension(400,400));
		
		add(myScrollPane);
		pack();
		setVisible(true);
		setPreferredSize(new Dimension(400,400));
	}
	
	private void defineVKDecoder()
	{
		VKDecoder = new HashMap<String,Integer>();
		VKDecoder.put("a",KeyEvent.VK_A);
		VKDecoder.put("b",KeyEvent.VK_B);
		VKDecoder.put("c",KeyEvent.VK_C);
		VKDecoder.put("d",KeyEvent.VK_D);
		VKDecoder.put("f",KeyEvent.VK_F);
		VKDecoder.put("g",KeyEvent.VK_G);
		VKDecoder.put("h",KeyEvent.VK_H);
		VKDecoder.put("j",KeyEvent.VK_J);
		VKDecoder.put("k",KeyEvent.VK_K);
		VKDecoder.put("l",KeyEvent.VK_L);
		VKDecoder.put("m",KeyEvent.VK_M);
		VKDecoder.put("n",KeyEvent.VK_N);
		VKDecoder.put("o",KeyEvent.VK_O);
		VKDecoder.put("p",KeyEvent.VK_P);
		VKDecoder.put("q",KeyEvent.VK_Q);
		VKDecoder.put("r",KeyEvent.VK_R);
		VKDecoder.put("s",KeyEvent.VK_S);
		VKDecoder.put("u",KeyEvent.VK_U);
		VKDecoder.put("v",KeyEvent.VK_V);
		VKDecoder.put("w",KeyEvent.VK_W);
		VKDecoder.put("x",KeyEvent.VK_X);
		VKDecoder.put("y",KeyEvent.VK_Y);
		VKDecoder.put("z",KeyEvent.VK_Z);
		
	}
	
	private class ControlDialogListener implements ActionListener
    {
            public void actionPerformed(ActionEvent e)
            {
            	String s = e.getActionCommand();
            	if (s == "north")
            	{
            		myBindable.changeBinding(VKDecoder.get(myComboBoxes.get(0).getSelectedItem()),ActionEnum.MOVE_NORTH);
            	}
            	else if (s == "northeast")
            	{
            		myBindable.changeBinding(VKDecoder.get(myComboBoxes.get(1).getSelectedItem()),ActionEnum.MOVE_NORTHEAST);
            	}
            	else if (s == "east")
            	{
            		myBindable.changeBinding(VKDecoder.get(myComboBoxes.get(2).getSelectedItem()),ActionEnum.MOVE_EAST);
            	}
            	else if (s == "southeast")
            	{
            		myBindable.changeBinding(VKDecoder.get(myComboBoxes.get(3).getSelectedItem()),ActionEnum.MOVE_SOUTHEAST);
            	}
            	else if (s == "south")
            	{
            		myBindable.changeBinding(VKDecoder.get(myComboBoxes.get(4).getSelectedItem()),ActionEnum.MOVE_SOUTH);
            	}
            	else if (s == "southwest")
            	{
            		myBindable.changeBinding(VKDecoder.get(myComboBoxes.get(5).getSelectedItem()),ActionEnum.MOVE_SOUTHWEST);
            	}
            	else if (s == "west")
            	{
            		myBindable.changeBinding(VKDecoder.get(myComboBoxes.get(6).getSelectedItem()),ActionEnum.MOVE_WEST);
            	}
            	else if (s == "northwest")
            	{
            		myBindable.changeBinding(VKDecoder.get(myComboBoxes.get(7).getSelectedItem()),ActionEnum.MOVE_NORTHWEST);
            	}
            	
            }
    }
	
}
